package com.example.calcitedemo;

import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.util.SqlVisitor;

import javax.annotation.Nullable;

/**
 * Default implementation of {@link ArgHandler} which merely calls
 * {@link SqlNode#accept} on each operand.
 *
 * @param <R> result type
 */
public class ArgHandlerImpl<R> implements ArgHandler<R> {
    private static final ArgHandler<?> INSTANCE = new ArgHandlerImpl<>();

    @SuppressWarnings("unchecked")
    public static <R> ArgHandler<R> instance() {
        return (ArgHandler<R>) INSTANCE;
    }

    @Override public R result() {
        return null;
    }

    @Override public R visitChild(
            SqlVisitor<R> visitor,
            SqlNode expr,
            int i,
            @Nullable SqlNode operand) {
        if (operand == null) {
            return null;
        }
        return operand.accept(visitor);
    }
}
